package top.lingkang.finalvalidated.handle;

import top.lingkang.finalvalidated.error.ValidatedException;

import java.util.List;

/**
 * 处理嵌套校验对象
 *
 * @author lingkang
 * Create by 2024/12/22 0:53
 * @since 2.3.0
 */
public class NestingValidHandle extends ValidHandle {
    private final List<ValidHandle> handles;
    private final String nullError;
    private final String tip;

    public NestingValidHandle(List<ValidHandle> handles, ValidVal validVal) {
        super(validVal, null);
        this.handles = handles;
        tip = validVal.getTag();
        nullError = tip + " 参数属性不能为空";
    }

    @Override
    public void valid(Object target) {
        Object val = validVal.getValGet().get(target);
        if (val == null)
            throw new ValidatedException(nullError);
        try {
            for (ValidHandle handle : handles) {
                handle.valid(val);
            }
        } catch (ValidatedException e) {
            e.setValidVal(validVal);
            e.setMessage(tip + " --> " + e.getMessage());
            throw e;
        }
    }

    @Override
    public Class<?>[] getGroups() {
        return new Class<?>[0];
    }
}
